// https://leetcode.cn/problems/summary-ranges/?envType=study-plan-v2&envId=top-interview-150

class Solution {
public:
    vector<string> summaryRanges(vector<int>& nums) {
        int n = nums.size();

        if (!n) {
            return vector<string>();
        }
        
        vector<string> ans;
        if (n == 1) {
            string num = to_string(nums[0]);
            ans.emplace_back(num);
            return ans;
        }

        int left = 0, right = 0;
        while (right < n) {
            if (right + 1 < n && nums[right + 1] == nums[right] + 1) {
                right++;
                continue;
            }

            if (left == right) {
                string word = to_string(nums[left]);
                ans.emplace_back(word);
            } else {
                string word = to_string(nums[left]);
                word += "->";
                word += to_string(nums[right]);
                ans.emplace_back(word);
            }

            right++;
            left = right;
        }
        return ans;
    }
};